//
//  HNSH_SendRedMessageCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/8/12.
//

static CGFloat imgWidth = 240;
static CGFloat imgHeight = 84;

#import "HNSH_SendRedMessageCell.h"
#import "HNSH_SendRedContent.h"

@interface HNSH_SendRedMessageCell ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/** */
@property (nonatomic, strong)UILabel * levelLabel;
/* iconImgV */
@property (nonatomic, strong) UIImageView *iconImgV;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* statusLabel */
@property (nonatomic, strong) UILabel *statusLabel;
/* lineView */
@property (nonatomic, strong) UIView *lineView;
/* logoNameLabel */
@property (nonatomic, strong) UILabel *logoNameLabel;

@end

@implementation HNSH_SendRedMessageCell

#pragma mark - Cycle Methods
+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight{
    return CGSizeMake(collectionViewWidth, extraHeight + imgHeight);
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.model.messageDirection == MessageDirection_SEND) {
        self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, 0, self.messageContentView.width, self.messageContentView.height);
        self.bgView.frame = CGRectMake(self.messageContentView.width - imgWidth, 0, imgWidth, imgHeight);
        self.nicknameLabel.hidden =  self.levelLabel.hidden = YES;
        self.bgView.frame = CGRectMake(self.messageContentView.width - imgWidth, 0, imgWidth, imgHeight);
    } else {
        if (self.model.conversationType == ConversationType_GROUP) {
            self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, self.nicknameLabel.height+4, self.messageContentView.width, self.messageContentView.height);
            self.nicknameLabel.hidden = self.levelLabel.hidden = NO;
            [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.nicknameLabel.mas_right).offset(6);
                make.centerY.equalTo(self.nicknameLabel.mas_centerY);
                make.width.mas_equalTo(32);
                make.height.mas_equalTo(14);
            }];
        } else {
            self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, 0, self.messageContentView.width, self.messageContentView.height);
            self.nicknameLabel.hidden =  self.levelLabel.hidden = YES;
        }
        self.bgView.frame = CGRectMake(0, 0, imgWidth, imgHeight);
    }
    
    RCUserInfo * userInfo = [[RCIM sharedRCIM] getUserInfoCache:self.model.senderUserId];
    if (userInfo) {
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    } else {
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [self.model.userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    }
}

- (void)setModel:(RCMessageModel *)model{
    [super setModel:model];
    
    HNSH_SendRedContent * con = (HNSH_SendRedContent *)model.content;
//    NSLog(@"NYSendRedContent ==> %@   %@ %@  %@  %ld",con.extra,con.senderUserInfo.extra,model.expansionDic,model.extra,model.canIncludeExpansion);
    
    self.desLabel.text = con.remark;
    
    NSLog(@"oldMess.expansionDic ==> %@",model.expansionDic);
    
    NSArray *ary = [model.expansionDic[@"openIds"] componentsSeparatedByString:@","];
    if ([ary containsObject:[ST_UserManager shareInstance].rcUser.uid] || con.status == 1) {
        self.bgView.backgroundColor = [UIColor colorWithHexString:@"#FDDEB6"];
        self.statusLabel.hidden = NO;
        self.iconImgV.image = [UIImage imageNamed:@"chat_red_select_off"];
        self.desLabel.frame = CGRectMake(self.desLabel.frame.origin.x, 12, self.desLabel.width, self.desLabel.height);
    } else {
        self.bgView.backgroundColor = [UIColor colorWithHexString:@"#FC972E"];
        self.statusLabel.hidden = YES;
        self.iconImgV.image = [UIImage imageNamed:@"chat_red_select_on"];
        self.desLabel.frame = CGRectMake(self.desLabel.frame.origin.x, 21, self.desLabel.width, self.desLabel.height);
    }
    
    //
    RCUserInfo * userInfo = [[RCIM sharedRCIM] getUserInfoCache:model.senderUserId];
    if (userInfo) {
        NSDictionary * dic = [userInfo.extra jsonValueDecoded];
        self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dic[@"level"] integerValue]];
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    } else {
        NSDictionary * dic = [model.userInfo.extra jsonValueDecoded];
        self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dic[@"level"] integerValue]];
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [model.userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    }
}

- (void)addAllViews {
    [self.messageContentView addSubview:self.levelLabel];
    [self.messageContentView addSubview:self.bgView];
    [self.bgView addSubview:self.iconImgV];
    [self.bgView addSubview:self.desLabel];
    [self.bgView addSubview:self.statusLabel];
    [self.bgView addSubview:self.lineView];
    [self.bgView addSubview:self.logoNameLabel];
}

- (void)gesAction{
//    NYSendRedContent * con = (NYSendRedContent *)self.model.content;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didTapMessageCell:)]) {
        [self.delegate didTapMessageCell:self.model];
    }
}

#pragma mark lazy
- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel new];
        _levelLabel.font = [UIFont systemFontOfSize:12];
        _levelLabel.textColor = [UIColor appWhiteLabelColor];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
        _levelLabel.backgroundColor = [UIColor colorWithHexString:@"#FB6F1C"];
        _levelLabel.layer.cornerRadius = 4.0f;
        _levelLabel.layer.masksToBounds = YES;
    }
    return _levelLabel;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(self.messageContentView.width - imgWidth, 0, imgWidth, imgHeight)];
        _bgView.backgroundColor = [UIColor colorWithHexString:@"#FC972E"];
        _bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
        [_bgView addGestureRecognizer:ges];
    }
    return _bgView;
}

- (UIImageView *)iconImgV {
    if (!_iconImgV) {
        _iconImgV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 12, 34, 40)];
        _iconImgV.image = [UIImage imageNamed:@"chat_red_select_on"];
    }
    return _iconImgV;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 12, 144, 22)];
        _desLabel.text = @"";
        _desLabel.textColor = [UIColor whiteColor];
        _desLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _desLabel;
}

- (UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 35, 144, 17)];
        _statusLabel.text = @"已领取";
        _statusLabel.textColor = [UIColor whiteColor];
        _statusLabel.font = [UIFont systemFontOfSize:12];
    }
    return _statusLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(17, 63, self.bgView.width-30, 1)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#FFE9CD"];
    }
    return _lineView;
}

- (UILabel *)logoNameLabel {
    if (!_logoNameLabel) {
        _logoNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, 65, 144, 17)];
        _logoNameLabel.text = @"悦享红包";
        _logoNameLabel.textColor = [UIColor whiteColor];
        _logoNameLabel.font = [UIFont boldSystemFontOfSize:12];
    }
    return _logoNameLabel;
}

@end
